import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:study_online/entity/MyCourseItem.dart';
import 'package:study_online/global/global.dart';
import 'package:study_online/pages/studyPage/views/StudyPageSkeleton.dart';

import '../StudyModel.dart';
import 'CourseItem.dart';

class RequiredCourseTab extends StatefulWidget {
  RequiredCourseTab({Key key}) : super(key: key);

  @override
  _RequiredCourseTabState createState() {
    return _RequiredCourseTabState();
  }
}

class _RequiredCourseTabState extends State<RequiredCourseTab> {
  MyCourseItemList _myCourseItemList = new MyCourseItemList([]);

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _loadData() async {
    var stuId = Global.getInstance().user.stuId;
    Response response = await getMyCourseListByIdType(stuId, "必修");
    // List responseData=json.decode(response.toString());
    print("###" + response.toString());
    setState(() {
      _myCourseItemList =
          MyCourseItemList.formJson(json.decode(response.toString()));
      print("###" + _myCourseItemList.list[0].toString());
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    if (_myCourseItemList.list.length == 0) {
      return EasyRefresh(
        enableControlFinishRefresh: false,
        enableControlFinishLoad: true,
        header: MaterialHeader(),
        footer: ClassicalFooter(),
        onRefresh: () async {
          _loadData();
        },
        onLoad: () async {
          _loadData();
        },
        child: StudyPageSkeleton(),
      );
    }
    return EasyRefresh.custom(
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      header: MaterialHeader(),
      onRefresh: () async {
        _loadData();
      },
      onLoad: () async {
        _loadData();
      },
      slivers: [
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, index) {
              return AnimationConfiguration.staggeredList(
                position: index,
                duration: const Duration(milliseconds: 1075),
                child: SlideAnimation(
                  verticalOffset: 0.5 * ScreenUtil().setHeight(372),
                  child: FadeInAnimation(
                    child: CourseItem(
                      index: index,
                      course: _myCourseItemList.list[index],
                    ),
                  ),
                ),
              );
            },
            childCount: _myCourseItemList.list.length,
          ),
        ),
      ],
    );
  }
}
